package tld.course.lesson2 
{
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author wvxvw
	 */
	public class Snake
	{
		public function get headX():uint { return this._head.x; }
		
		public function get headY():uint { return this._head.y; }
		
		public function get lastX():uint
		{
			return this._tail[this._tail.length - 1].x;
		}
		
		public function get lastY():uint
		{
			return this._tail[this._tail.length - 1].y;
		}
		
		public function get length():uint { return this._tail.length; }
		
		private const _tail:Vector.<Point> = new <Point>[];
		private const _head:Point = new Point();
		
		private var _iterator:int;
		
		public function Snake(x:uint, y:uint) 
		{
			super();
			this._head.x = x;
			this._head.y = y;
			this._tail.push(this._head);
		}
		
		public function next():Point
		{
			var result:Point;
			if (this._iterator < this._tail.length)
				result = this._tail[this._iterator++];
			return result;
		}
		
		public function reset():void { this._iterator = 0; }
		
		public function move(direction:uint, eat:Boolean = false):void
		{
			this._tail.unshift(this._head.clone());
			if (!eat) this._tail.pop();
			if (direction & 1) this._head.x += (direction % 3) - 1;
			if (uint(direction / 3) != 1)
				this._head.y += uint(direction / 3) - 1;
		}
	}
}